<%# coding: UTF-8 %><%
class Zim
  attr_reader :home, :min_zsh_version, :ellipsis, :okay, :warn, :error, :done, :failed

  def initialize
    @home = "${ZDOTDIR:-${HOME}}"
    @min_zsh_version = "5.2"
    @ellipsis = " ..."
    @okay = "%F{green})%f "
    @warn = "! "
    @error = "x "
    @done = ""
    @failed = ""
  end

  def render(filename)
    ERB.new(File.read(filename)).result(binding)
  end

  def render_all(pattern)
    Dir[pattern].sort.map { |filename| render(filename) }.join("\n")
  end

  def render_template(filename)
    render(filename)
      .gsub(/\\/, "\\\\\\\\")
      .gsub(/(?=\$[^']|"|`)/, "\\\\")
      .gsub(/\\\${ZDOTDIR:-\\\${HOME}}\/\.zim\b/, "${ZIM_HOME_STR}")
  end

  def render_templates(pattern)
    Dir[pattern].sort.map { |filename|
      "ZTEMPLATES[#{File.basename(filename)}]=\"#{render_template(filename)}\""
    }.join("\n")
  end

  def render_commented(filename)
    render(filename).gsub(/^(?=.)/, "# ").gsub(/^$/, "#")
  end
end
zim = Zim.new
%># AUTOMATICALLY GENERATED FILE. EDIT ONLY THE SOURCE FILES AND THEN COMPILE.
# DO NOT DIRECTLY EDIT THIS FILE!

<%= zim.render_commented("LICENSE") %>
<%= zim.render_all("src/steps/*.erb") %>
